﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Web;
  namespace Base.Common.httpServer {
    public class Utils {
        private const int BufSize = 4096;
        /// <summary>
        ///     输出200状态
        /// </summary>
      
        public static string ReadLine(Stream _inputStream) {
            int next_char;
            var data = "";
            int index = 1000;
            while(true) {
                next_char = _inputStream.ReadByte();
                if(next_char == '\n') {
                    break;
                }
                if(next_char == '\r') {
                    continue;
                }
                if(next_char == -1) {
                    index--;
                    if( index < 0 ) {
                        break;
                    } else {
                        continue;
                    }
                   
                }
                ;
                data += Convert.ToChar(next_char);
            }
            return data;
        }
        public static void getData(string rawData,Dictionary<string ,object> rets) {
       
            var rawParams = rawData.Split('&');
            foreach(var param in rawParams) {
                var kvPair = param.Split('=');
                var key = kvPair[0];
                var value = HttpUtility.UrlDecode(kvPair[1]);
                rets[key] = value;
            }
          
        }
        public static Hashtable ParseRequest(Stream _inputStream) {
            Hashtable HttpHeaders = new Hashtable();
            string line;
            while((line = ReadLine(_inputStream)) != null) {
                if(line.Equals("")) {
                    break;
                }
                var separator = line.IndexOf(':');
                if(separator == -1) {
                    continue;
                }
                var name = line.Substring(0,separator);
                var pos = separator + 1;
                while((pos < line.Length) && (line[pos] == ' ')) {
                    pos++; //过滤键值对的空格
                }
                var value = line.Substring(pos,line.Length - pos);
                HttpHeaders[name] = value;
              
            }
          
            return HttpHeaders;
        }
        //读取文件
        public static  string PostRequestExec(Hashtable HttpHeaders,Stream _inputStream) {
         
            var ms = new MemoryStream();
            if(HttpHeaders.ContainsKey("Content-Length")) {
                //内容的长度
                int content_len = Convert.ToInt32(HttpHeaders["Content-Length"]);
                var buf = new byte[BufSize];
                var to_read = content_len;
                while(to_read > 0) {
                    var numread = _inputStream.Read(buf,0,Math.Min(BufSize,to_read));
                    if(numread == 0) {
                        if(to_read == 0) {
                            break;
                        }
                        throw new Exception("client disconnected during post");
                    }
                    to_read -= numread;
                    ms.Write(buf,0,numread);
                }
                ms.Seek(0,SeekOrigin.Begin);
            }
            var inputData = new StreamReader(ms);
            return  inputData.ReadToEnd();
            //return Utils.getData(data);
        }
    }
}
